package com.xuerhua.yxt.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xuerhua.yxt.dao.FinalTermScoreReportMapper;
import com.xuerhua.yxt.dao.StudentInfoAndScoreMapper;
import com.xuerhua.yxt.model.StudentInfoAndScore;
import com.xuerhua.yxt.view.FinalTermScoreReportView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.dao.StudentInfoMapper;
import com.xuerhua.yxt.dao.StudentSchoolPrizeMapper;
import com.xuerhua.yxt.model.FinalTermScoreReport;
import com.xuerhua.yxt.model.StudentInfo;
import com.xuerhua.yxt.reqVo.BaseQueryVO;
import com.xuerhua.yxt.reqVo.StudentReqVO;
import com.xuerhua.yxt.service.IFinalTermScoreReportService;
import com.xuerhua.yxt.utils.StringUtil;

@Service("studentTermScoreReportService")
public class FinalTermScoreReportServiceImpl implements IFinalTermScoreReportService {

    @Autowired
    private FinalTermScoreReportMapper studentTermScoreReportMapper;
    @Autowired
    private StudentInfoMapper studentInfoMapper;
    @Autowired
    private StudentSchoolPrizeMapper studentSchoolPrizeMapper;
    //huhekun
    @Autowired
    private StudentInfoAndScoreMapper studentInfoAndScoreMapper;

    @Override
    public int insertSelective(FinalTermScoreReport record) {
        return studentTermScoreReportMapper.insertSelective(record);
    }

    @Override
    public FinalTermScoreReport selectByPrimaryKey(Integer id) {
        return studentTermScoreReportMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(FinalTermScoreReport record) {
        return studentTermScoreReportMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<FinalTermScoreReport> selectClassTermReport(BaseQueryVO vo) {
        return studentTermScoreReportMapper.selectClassTermReport(vo);
    }

    @Override
    public FinalTermScoreReport selectStudentTermReport(BaseQueryVO vo) {
        return studentTermScoreReportMapper.selectStudentTermReport(vo);
    }

    @Override
    public int delClassTermReport(BaseQueryVO vo) {
        return studentTermScoreReportMapper.delClassTermReport(vo);
    }

    @Override
    public int insertTermScoreReport(List<FinalTermScoreReportView> list) {
        return studentTermScoreReportMapper.insertTermScoreReport(list);
    }

    @Override
    public RspResult addImportTermScoreReport(List<FinalTermScoreReportView> list, BaseQueryVO vo) {
        StudentReqVO studentReqVO = new StudentReqVO();
        studentReqVO.setGrade(vo.getGrade());
        studentReqVO.setClazz(vo.getClazz());
        studentReqVO.setSchoolId(vo.getSchoolId());
        List<StudentInfo> studentList = studentInfoMapper.selectListByParam(studentReqVO);  //选出学生信息
        List<String> errorList = new ArrayList<>();
        Map<String, Integer> map = getClassStudentMap(studentList); //key:stuNum+stuName, value:stuId
        List<FinalTermScoreReportView> filterList = new ArrayList<>();
        for (FinalTermScoreReportView report : list) {
            if (StringUtil.isNotNullOrZero(report.getStudentNum())) {   //stuNum非空或0才进去
                String key = report.getStudentNum() + report.getStudentName();
                if (!map.containsKey(key)) {    //如果map里面没有这个key，则进入if报错，因为里面没有这个学生的成绩
                    errorList.add("学号" + report.getStudentNum() + "的学生姓名与班级学生匹配不上");
                    continue;
                }
                report.setStudentId(map.get(key));  //根据key设stuId
                report.setTermId(vo.getTermId());
                filterList.add(report); //把拥有stuId和termId的对象放到list里面（就是说这个filterList里面都是一个个的对象）
            }
        }
        if (!StringUtil.isNullOrEmpty(errorList)) { //如果出错列表有内容则进去
            return new RspResult(Status.FAIL.getCode(), String.join(",", errorList));
        }
        studentTermScoreReportMapper.delClassTermReport(vo);
        List<FinalTermScoreReportView> insertList = new ArrayList<>();
        for (int i = 0; i < filterList.size(); i++) {
            insertList.add(filterList.get(i));  //把filterList里面的对象放到insertList里面
            if ((i + 1) % 30 == 0) {
                studentTermScoreReportMapper.insertTermScoreReport(insertList);
                insertList = new ArrayList<>();
            }
        }
        if (!StringUtil.isNullOrEmpty(insertList)) {
            studentTermScoreReportMapper.insertTermScoreReport(insertList);
        }
        studentSchoolPrizeMapper.delStudentTermPrize(vo);
        studentSchoolPrizeMapper.insertStudentTermPrize(vo);
        return new RspResult(Status.SUCCESS);
    }

    /**
     * 查询班级学生map
     * key: 姓名+学号 value: ID
     *
     * @param list, class
     * @return
     */
    private static Map<String, Integer> getClassStudentMap(List<StudentInfo> list) {
        Map<String, Integer> map = new HashMap<>();
        for (StudentInfo student : list) {
            String key = student.getStudentNum() + student.getName();
            //没有这个key才进入if
            if (!map.containsKey(key)) {
                map.put(key, student.getId());  //map的内容看注释
            }
        }
        return map;
    }

    /**
     * @author huhekun
     * @date 2020/1/2
     * @description 用于查询导出Excel表所需要的信息
     */
    @Override
    public List<StudentInfoAndScore> selectInfoByTermidAndGradeAndClazz(StudentReqVO vo) {
        return studentInfoAndScoreMapper.selectStudentInfoAndScore(vo);
    }
}
